﻿using System;
using System.Json;
using System.Windows.Browser;
using System.Xml.Linq;

namespace jLight
{

    public class jQueryFunction
    {
        private readonly Guid _guid;
        private string _functionName;

        Func<object, object> _action1;
        Func<object, object, object> _action2;
        Func<jQueryObject, int, string, string> _actionjQueryObject;
        Action<JsonValue> _actionJson;
        Action<XDocument> _actionXml;

        public jQueryFunction()
        {
            _guid = Guid.NewGuid();
            _functionName = "_" + _guid.ToString().Replace("-", "");

            HtmlPage.RegisterScriptableObject(_functionName, this);

        }

        public static ScriptObject CreateCallback1(Func<object, object> action)
        {
            jQueryFunction j = new jQueryFunction();
            j._action1 = action;

            return (ScriptObject)HtmlPage.Window.Eval(
                string.Format("[function(){{return document.getElementById('{0}').Content.{1}.Callback1(jQuery(this));}}][0]",
                HtmlPage.Plugin.Id, j._functionName));
        }

        public static ScriptObject CreateCallback2(Func<object, object, object> action)
        {
            jQueryFunction j = new jQueryFunction();
            j._action2 = action;
            return (ScriptObject)HtmlPage.Window.Eval(
                string.Format("[function(index){{return document.getElementById('{0}').Content.{1}.Callback2(jQuery(this),index);}}][0]",
                HtmlPage.Plugin.Id, j._functionName));
        }
        public static ScriptObject CreateCallbackJson(Action<JsonValue> action)
        {
            jQueryFunction j = new jQueryFunction();
            j._actionJson = action;

            return (ScriptObject)HtmlPage.Window.Eval(
                string.Format("[function(data){{return document.getElementById('{0}').Content.{1}.CallbackJson(data);}}][0]",
                HtmlPage.Plugin.Id, j._functionName));
        }

        public static ScriptObject CreateCallbackString(Action<XDocument> action)
        {
            jQueryFunction j = new jQueryFunction();
            j._actionXml = action;

            return (ScriptObject)HtmlPage.Window.Eval(
                string.Format("[function(data){{return document.getElementById('{0}').Content.{1}.CallbackString(data);}}][0]",
                HtmlPage.Plugin.Id, j._functionName));
        }
        public static ScriptObject CreateCallbackjQueryObject(Func<jQueryObject, int,string, string> function)
        {
            jQueryFunction j = new jQueryFunction();
            j._actionjQueryObject = function;

            return (ScriptObject)HtmlPage.Window.Eval(
                string.Format("[function(index, data){{return document.getElementById('{0}').Content.{1}.CallbackjQueryObject(jQuery(this),index,data);}}][0]",
                HtmlPage.Plugin.Id, j._functionName));
        }
        [ScriptableMember]
        public object Callback1(object a)
        {
            return _action1(a);
        }

        [ScriptableMember]
        public object Callback2(object a, object arg)
        {
            return _action2(a, arg);
        }

        [ScriptableMember]
        public void CallbackJson(string a)
        {
            JsonValue j = JsonValue.Parse(a.Replace("\\", ""));
            _actionJson(j);
        }

        [ScriptableMember]
        public void CallbackString(string args)
        {
            _actionXml(XDocument.Parse(args));
        }

        [ScriptableMember]
        public string CallbackjQueryObject(object args, int args2, string className)
        {
            return _actionjQueryObject((ScriptObject)(args), args2, className);
        }
    }

}